package controller

import (
	"errors"
	"haiqiu-go-admin/result"
	"haiqiu-go-admin/service"
	"haiqiu-go-admin/utils"
	"log"
	"net/http"
	"strconv"
)

func (*UserApi) UserHandle(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodGet:
		UserAPI.UserById(w, r)
		break
	case http.MethodPost:
		UserAPI.userAdd(w, r)
		break
	case http.MethodPut:
		UserAPI.userUpdate(w, r)
		break
	case http.MethodDelete:
		UserAPI.userDeleteBatch(w, r)
		break
	default:
		break
	}
}

func (*UserApi) UserInfo(w http.ResponseWriter, r *http.Request) {
	_, userId, _, err := utils.Token{}.GetUserTokenAndUserIdAndUsername(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	user, err := service.UserById(*userId)
	if err != nil {
		result.Fail(w, r, errors.New("用户不存在"))
		return
	}
	result.Ok(w, r, user)
}

func (*UserApi) UserById(w http.ResponseWriter, r *http.Request) {
	formatJson := utils.GetParamsFormatJson(w, r)
	userIdStr := formatJson["id"]
	if userIdStr == nil {
		result.Fail(w, r, errors.New(result.ParamsErr))
		return
	}
	userId, err := strconv.ParseInt(userIdStr[0], 10, 64)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	user, err := service.UserById(userId)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, user)
}

func (*UserApi) UserPage(w http.ResponseWriter, r *http.Request) {
	page, err := service.UserPage(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

func (*UserApi) userAdd(w http.ResponseWriter, r *http.Request) {
	add, err := service.UserAdd(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

func (*UserApi) userDeleteBatch(w http.ResponseWriter, r *http.Request) {
	add, err := service.UserDeleteBatch(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

func (*UserApi) userUpdate(w http.ResponseWriter, r *http.Request) {
	add, err := service.UserUpdate(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

func (*UserApi) UserList(w http.ResponseWriter, r *http.Request) {
	userList, err := service.UserList(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, userList)
}

// 根据用户ID授权用户角色
func (*UserApi) AuthRole(w http.ResponseWriter, r *http.Request) {
	role, err := service.AuthRole(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, role)
}

// 激活禁用(用户)
func (*UserApi) Active(w http.ResponseWriter, r *http.Request) {
	active, err := service.Active(w, r)
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, active)
}

// 后台管理重置用户密码
func (*UserApi) ResetPwd(w http.ResponseWriter, r *http.Request) {
	data, err := service.ResetPwd(w, r)
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, data)
}

func (*UserApi) AvatarUpload(w http.ResponseWriter, r *http.Request) {
	data, err := service.AvatarUpload(w, r)
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, data)
}
